<?php

        class Vezir {

            # Just Vezir Operations can use Connection.
            private $Connection;

            # 0 - to don't show any message.
            # 2 - to just show errors.
            # 3 - to show all actions.
            public $DbLogDetail;

            # Table Prefix
            public $Prefix = "Comanche_";
            public $UsePrefix = True;

            # General
            public $ConsoleOutput = False;

            function Vezir($Conf="",$ConsoleOutput=False){
                if (is_array($Conf)) {
                    # Set the variables
                    $this->DbLogDetail=$Conf["LogLevel"];
                    $this->ConsoleOutput=$ConsoleOutput;

                    if (array_key_exists("Prefix",$Conf))
                        $this->Prefix=$Conf["Prefix"];
                    if (array_key_exists("UsePrefix",$Conf))
                        $this->UsePrefix=$Conf["UsePrefix"];

                    # Make Connection
                    $this->DbConnect($Conf["Host"],$Conf["User"],$Conf["Pass"],$Conf["Data"]);
                }
                else
                    $this->ShowError("No Conf data given !!");
            }

            function CheckConnection(){
                if ($this->Connection) return true; return false;
            }

            function DbConnect($DbHost,$DbUser,$DbPass,$DbData=''){
                try {
                    $this->Connection = @mysql_connect($DbHost,$DbUser,$DbPass,$DbData);
                    if (!$this->Connection)
                        throw new Exception('Connection Error',2);
                    elseif ($this->DbLogDetail>3)
                        $this->ParseError("Connected to $DbHost");
                    if ($DbData<>"")
                        if (@mysql_select_db($DbData) AND $this->DbLogDetail>3)
                            $this->ParseError("Selected db: $DbData");
                        else
                            throw new Exception('Database Error',8);
                }

                catch (Exception $Ex) {
                    if ($this->DbLogDetail>1)
                        $this->ShowError($Ex,mysql_error());
                    else
                        $this->ParseError($Ex);
                }
            }

            function ShowError($Ex,$Note='') {
                if ($this->DbLogDetail>1) {
                    $out.= '<pre><b>';
                    $out.= 'DEBUG: Exception '.$Ex->getCode().' : '.$Ex->getMessage()."<br>";
                    if ($this->DbLogDetail>4) $out.= 'DEBUG: File        : '.$Ex->getFile().' : '.$Ex->getLine()."<br>";
                    if ($Note<>"") $out.= 'DEBUG: Note        : '.$Note.'<br>';
                    $out.= 'DEBUG: Exiting...';
                    $out.= '</b></pre>';
                    if ($this->ConsoleOutput==False)
                        echo $out;
                    else
                        $this->ConsoleMessages = $out;
                }
                else $this->ParseError("An error occured. Exiting.. <br /> Tip: If you want to see full error report; set LogLevel > 4.");
            }

            function UpdateField($Table,$Field,$Value,$ID='') {
                $Table=$this->Pref_($Table);
                $ID == "" ? $AddSql = "" : $AddSql = "WHERE ID=$ID";
                $Sql = "UPDATE $Table SET $Field='$Value' ".$AddSql;
                $this->ExecuteQuery($Sql);
            }

            function Pref_($Table) {
                if ($this->UsePrefix==true)
                    return $this->Prefix.$Table;
                return $Table;
            }

            function UpdateArray($Table,$Fields,$Values,$ID) {
                $Table=$this->Pref_($Table);
                if ($ID) {
                    $Sql = "UPDATE $Table SET ";
                    foreach ($Fields as $Key=>$Field)
                        $Sql .= $Field."='".$Values[$Key]."',";
                    $Sql = rtrim($Sql,",")." WHERE ID=".$ID;
                    if ($this->ExecuteQuery($Sql)) return true;
                }
                return false;
            }

            function InsertRecord($Table,$Fields,$Values) {
                $Table=$this->Pref_($Table);
                $Sql = "INSERT INTO $Table (";
                foreach ($Fields as $FValue) {
                    $Sql .= $FValue.",";
                }
                $Sql = rtrim($Sql,",");
                $Sql .= ") VALUES (";
                foreach ($Values as $VValue) {
                    $Sql .= "'".$VValue."'".",";
                }
                $Sql = rtrim($Sql,",");
                $Sql .=")";
                $this->ExecuteQuery($Sql);
                return mysql_insert_id();
            }

            function DeleteRecord($Table,$ID) {
                $Table=$this->Pref_($Table);
                $Sql = "DELETE FROM $Table WHERE ID=$ID";
                return $this->ExecuteQuery($Sql);
            }

            function ExecuteQuery($Sql) {
                try {
                    $Result = mysql_query($Sql,$this->Connection);
                    if (!$Result)
                        throw new Exception(mysql_error(),3);
                    elseif ($this->DbLogDetail>2){
                        if (strlen($Sql)>100) $Sql = substr($Sql, 0, 90)."...";
                        $this->ParseError("Query Executed Sucessfully : ".$Sql);
                    }
                    return $Result;
                }
                catch (Exception $Ex) {
                    $this->ShowError($Ex,$Sql);
                    return false;
                }
            }

            function ParseError($Message) {
                $this->MessageQueue.= "<pre><b>DEBUG : ".$Message."</b></pre>";
            }

            function GetRecord($Table,$Field='*',$ID='',$Ext='') {
                $Table=$this->Pref_($Table);
                $ID == "" ? $AddSql = "" : $AddSql = "WHERE ID=$ID";
                $Ext == "" ? $AddSql = $AddSql: $AddSql = $AddSql." ".$Ext;
                $Sql = "SELECT $Field FROM $Table ".$AddSql;
                $Result = $this->ExecuteQuery($Sql);
                return $this->MakeArray($Result);
            }

            function FindRecord($Table,$Field,$Value,$ReturnValue='ID') {
                $Table=$this->Pref_($Table);
                $Sql = "SELECT $ReturnValue FROM $Table WHERE $Field LIKE '%$Value%'";
                $Result = $this->ExecuteQuery($Sql);
                return $this->MakeArray($Result);
            }

            function MakeArray($Raw) {
                $i=0;
                while ($Row = @mysql_fetch_array($Raw, MYSQL_ASSOC)) {
                    foreach ($Row as $RKey => $RValue)
                        $ReturnValue[$i][$RKey] = $RValue;
                    $i++;
                }
                if ($i==0)
                    $ReturnValue = 0;
                @mysql_free_result($Raw);
                return $ReturnValue;
            }

            function ShowLogs($Opt=0) {
                if ($Opt)
                    return $this->MessageQueue;
                else
                    echo $this->MessageQueue;
            }

        }

?>
